//
//  MapDirectionViewController.m
//  iShop
//
//  Created by svp on 21/12/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MapDirectionViewController.h"
#import "ConstansDefine.h"
#import "Utils.h"


@implementation MapDirectionViewController

@synthesize webView = _webView;
@synthesize shopAddress = _shopAddress;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void) viewDidLoad
{
	[super viewDidLoad];
	_coordinate = [[NSMutableArray alloc] init];
	flag = YES;
	[_coordinate addObject:[NSString stringWithFormat:@"%@,%@",[Utils getValueForRefKey:kLatitude],[Utils getValueForRefKey:kLogitude]]];
	[_coordinate addObject:_shopAddress];
	NSString *urlReq = [NSString stringWithFormat:@"http://maps.google.com/maps?f=d&hl=en&geocode=&saddr=%@&daddr=%@&sll=%@&sspn=0.002798,0.005284&ie=UTF8&z=14&output=embed",flag?[_coordinate objectAtIndex:0]:[_coordinate objectAtIndex:1],flag?[_coordinate objectAtIndex:1]:[_coordinate objectAtIndex:0],flag?[_coordinate objectAtIndex:0]:[_coordinate objectAtIndex:1]];
	NSLog(@"test:%@",urlReq);
	[_webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlReq]]];
	exist = NO;
}

-(IBAction) savePress:(id) sender;
{
	NSLog(@"savePress");
	[self.parentViewController dismissModalViewControllerAnimated:YES];
}

-(IBAction) revertPress:(id) sender
{
	flag = !flag;
	NSString *urlReq = [NSString stringWithFormat:@"http://maps.google.com/maps?f=d&hl=en&geocode=&saddr=%@&daddr=%@&sll=%@&sspn=0.002798,0.005284&ie=UTF8&z=14&output=embed",flag?[_coordinate objectAtIndex:0]:[_coordinate objectAtIndex:1],flag?[_coordinate objectAtIndex:1]:[_coordinate objectAtIndex:0],flag?[_coordinate objectAtIndex:0]:[_coordinate objectAtIndex:1]];
	[_webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlReq]]];
	exist = NO;
}

- (void)dealloc {
	[_webView release];
    [super dealloc];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	if (exist) {
		return;
	}
	exist = YES;
	NSString *distance = [webView stringByEvaluatingJavaScriptFromString: @"document.getElementById('altroute_0').getElementsByTagName('span')[0].textContent"];	
	NSString *time = [webView stringByEvaluatingJavaScriptFromString: @"document.getElementById('altroute_0').getElementsByTagName('span')[1].textContent"];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Infomation" message:[NSString stringWithFormat:@"Distance: %@\nTime: %@",distance,time] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
	[alert show];
	[alert release];

}


@end
